 package com.server;
 
 import cn.com.code.MyDecoder;
 import cn.com.code.MyEncoder;
 import cn.com.common.ConnectionPool;
 import cn.com.user.Poke_User;
 import com.server.thread.ConnectionThread;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
 import java.io.IOException;
 
 import java.net.InetSocketAddress;
 import java.util.HashMap;
 import java.util.Map;
import java.util.Properties;
 import java.util.concurrent.Executor;
 import java.util.concurrent.Executors;
 
 import org.apache.mina.core.service.IoAcceptor;
 import org.apache.mina.core.session.IdleStatus;
 
 import org.apache.mina.filter.codec.ProtocolCodecFilter;
 import org.apache.mina.filter.executor.ExecutorFilter;
 import org.apache.mina.filter.logging.LogLevel;
 import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
 
 public class PokeServer
 {
   public static Map<Integer, Board_Table> tablemap = new HashMap<Integer, Board_Table>();
   public static Map<String, Poke_User> usermap = new HashMap<String, Poke_User>();
   public static Map<Integer, ConnectionThread> threadmap = new HashMap<Integer, ConnectionThread>();
   public static int PORT = 9090;
   private static String IPADDRESS = "";
//   private static boolean REUSEADDRESS = true;
   private InetSocketAddress inetSocketAddress = null;
   private IoAcceptor acceptor = null;
 
   private static PokeServer singleInstance = null;
   public static int money = 50000;
 
   public static PokeServer getInstance()
     throws Exception
   {
	     if (singleInstance == null)
	     {
	       singleInstance = new PokeServer();
	     }
	     return singleInstance;
   }
 
   public void startup()
     throws IOException, Exception
   {
     try
     {
	       initConfig();
	       this.acceptor = new NioSocketAcceptor();
	 
	       LoggingFilter filter = new LoggingFilter();
	 
	       filter.setExceptionCaughtLogLevel(LogLevel.INFO);
	       filter.setMessageReceivedLogLevel(LogLevel.INFO);
	       filter.setMessageSentLogLevel(LogLevel.INFO);
	       filter.setSessionClosedLogLevel(LogLevel.INFO);
	       filter.setSessionCreatedLogLevel(LogLevel.INFO);
	       filter.setSessionIdleLogLevel(LogLevel.INFO);
	       filter.setSessionOpenedLogLevel(LogLevel.INFO);
	 
	       this.acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 150);
	       this.acceptor.getSessionConfig().setMinReadBufferSize(1024);
	 
	       Executor threadPool = Executors.newCachedThreadPool();
	       this.acceptor.getFilterChain().addLast("exector", new ExecutorFilter(threadPool));
	       this.acceptor.getFilterChain().addLast(
	         "codec", 
	         new ProtocolCodecFilter(new MyEncoder(), new MyDecoder()));
	       
	       
	       
	       if ((IPADDRESS != null) && (!IPADDRESS.equals("")))
	       {
	         this.inetSocketAddress = new InetSocketAddress(IPADDRESS, PORT);
	       }
	       else 
	       {
	         this.inetSocketAddress = new InetSocketAddress(PORT);
	       }
	       this.acceptor.setHandler(new PokeServerHandle());
	       this.acceptor.bind(this.inetSocketAddress);
	 
	       cn.com.usermanage.impl.UserManagerImpl.conpool = ConnectionPool.getInstance();
     }
     catch (IOException e)
     {
	       e.printStackTrace();
	       throw new IOException("终端服务启动失败:" + e.getMessage());
     }
     catch (Exception e)
     {
    	 throw new Exception("终端服务启动失败:" + e.getMessage());
     }
   }
 
   private void initConfig()
   {   
	   Properties ps = new Properties();
		FileInputStream fis;
		try 
		{
			fis = new FileInputStream("properties/server.properties");
			ps.load(fis);
			fis.close();
		} catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		} catch (IOException e) 
		{
			e.printStackTrace();
		}
		PORT = Integer.parseInt(ps.getProperty("port"));
		IPADDRESS = ps.getProperty("ipaddress");
		ps.clear();
		
//     REUSEADDRESS = true;
   }
 }

